import { defineStore } from 'pinia'
import { getLanguage, setLanguage } from '@/utils/cache'
import { useMenuStore } from '@/store/modules/menu'
import { useUserStore } from '@/store/modules/user'
import { useCodeStore } from '@/store/modules/code'
import { Language, ViewHome } from '@/enums'
import { removeToken } from '@/utils/cookie'
// import { i18n } from '@/i18n'

export const useAppStore = defineStore('app', {
    state() {
        return {
            init: false,//初始化状态
            language: '' as string,//国际化
            isRouterAlive: true,//刷新使用
            isDarkTheme: false,//切换深浅色主题
            collapsed: false,//菜单折叠
            tags: [] as Array<Tag>//标签页
        }
    },
    getters: {},
    actions: {
        async Init(){
            this.init = false
            await useUserStore().InitUserInfo()
            await useMenuStore().InitMenuInfo()
            await useCodeStore().InitCodeInfo()
            this.InitLanguage()
            this.InitTag()
            this.init = true
        },
        SwitchCollapsed(){
            this.collapsed = !this.collapsed
        },
        InitLanguage(){
            const language = getLanguage()
            this.language = language
            // i18n.global.locale = this.language
            setLanguage(this.language)
        },
        InitTag(){
            this.tags = [{ name: ViewHome.Name, path: ViewHome.Path }]
        },
        ChangeLanguage(language: Language){
            this.language = language
            // i18n.global.locale = language
            setLanguage(language)
        },
        AddTag(tag: Tag){
            const exists = this.tags.some( item => item.path === tag.path )
            if(exists) return
            this.tags.push(tag)
        },
        RemoveTag(tag: Tag){
            const index = this.tags.findIndex(item => item === tag)
            this.tags.splice(index, 1)
        },
        destory(){
            removeToken()
            useMenuStore().destory()
            this.init = false
        }
    }
})
